
using System;
using System.Linq;
using System.Collections.Generic;
using BLToolkit.DataAccess;
using BLToolkit.Mapping;
using BLToolkit.Data.Linq;
namespace Core
{
    [TableName("tblCanBo_TangLuong")]
    public class CanBoTangLuong
    {
        [Identity, PrimaryKey(1)]
        [MapField("MaCBTL")]
        public int ID { get; set; }
        public int MaCanBo { get; set; }
        public int MaQTTangLuong { get; set; }
        public int MaNgach { get; set; }
        public int Bac { get; set; }

        // FK_tblCanBo_TangLuong_tblQTTangLuong
        [Association(ThisKey = "MaQTTangLuong", OtherKey = "ID", CanBeNull = false)]
        public QuaTrinhTangLuong QuaTrinhTangLuong { get; set; }
        // FK_tblCB_TL_tblCanBo
        [Association(ThisKey = "MaCanBo", OtherKey = "ID", CanBeNull = false)]
        public CanBo CanBo { get; set; }

        // FK_tblCB_TL_tblCanBo
        [Association(ThisKey = "MaNgach", OtherKey = "ID", CanBeNull = false)]
        public NgachCongChuc NgachCongChuc { get; set; }

        public static IQueryable<CanBoTangLuong> CanBoTangLuongs
        {
            get
            {
                using (var db = new DataContext())
                {
                    return db.GetTable<CanBoTangLuong>();
                }
            }
        }
        public static List<CanBoTangLuong> All()
        {
            return CanBoTangLuongs.Select(x => new CanBoTangLuong
            {
                ID = x.ID,
                Bac = x.Bac,
                MaNgach = x.MaNgach,
                MaCanBo = x.MaCanBo,
                MaQTTangLuong = x.MaQTTangLuong,
                CanBo = x.CanBo,
                QuaTrinhTangLuong = x.QuaTrinhTangLuong,
                NgachCongChuc=x.NgachCongChuc
            }).ToList();
        }
        public static CanBoTangLuong Single(int id)
        {
            return CanBoTangLuongs.SingleOrDefault(x => x.ID == id);
        }
        public static int Add(CanBoTangLuong entity)
        {
            using (var db = new DataContext())
            {
                object rs = db.InsertWithIdentity(entity);
                return int.Parse(rs.ToString());
            }
        }
        public static int Update(CanBoTangLuong entity)
        {
            using (var db = new DataContext())
            {
                return db.Update(entity);
            }
        }
        public static int Delete(int id)
        {
            return CanBoTangLuongs.Delete(x => x.ID == id);
        }
    }
}